﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimplexEngine
{
    internal class ResourceFactory
    {

        /// <summary>
        /// load resource
        /// </summary>
        /// <param name="path">resource path</param>
        /// <returns>resource</returns>
        public Object LoadResource(String path)
        {
            String extension = System.IO.Path.GetExtension(path).ToUpper();

            if (loaders.ContainsKey(extension))
                return loaders[extension].LoadResource(path);

            return null;
        }

        /// <summary>
        /// get resource type by filename
        /// </summary>
        /// <param name="filename">resource filename</param>
        /// <returns>resource type</returns>
        public ResourceType GetResourceType(String filename)
        {
            String extension = System.IO.Path.GetExtension(filename).ToUpper();

            if (loaders.ContainsKey(extension))
                return loaders[extension].GetResourceType();

            return ResourceType.NONE;
        }

        /// <summary>
        /// add a new resource loader
        /// </summary>
        /// <param name="loader">resource loader to add</param>
        public void AddResourceLoader(IResourceLoader loader)
        {
            List<String> support_formats = loader.GetSupportFormats();
            foreach (String support_format in support_formats)
            {
                if (!loaders.ContainsKey(support_format))
                    loaders.Add(support_format.ToUpper(), loader);
            }
        }

        private Dictionary<String, IResourceLoader> loaders = new Dictionary<string, IResourceLoader>();
    }
}
